﻿using System;
using System.Text;

namespace Rodunu.Services.Amazon
{
    public class ServiceBinding
    {
        #region "Service Configuration"

        /// <summary>
        /// Gets the EndpointAddress object for the HttpBinding
        /// </summary>
        /// <param name="uri">URI of the endpoint</param>
        /// <returns>EndpointAddress for HttpBinding</returns>
        public System.ServiceModel.EndpointAddress InitEndPoint()
        {
            System.ServiceModel.EndpointAddress address =
                new System.ServiceModel.EndpointAddress("http://soap.amazon.com/onca/soap?Service=AWSECommerceService");
            return address;
        }

        /// <summary>
        /// Gets the binding to use for the contract
        /// </summary>
        /// <returns>BasicHttpBinding for a contract</returns>
        public System.ServiceModel.BasicHttpBinding InitBinding()
        {
            System.ServiceModel.BasicHttpBinding binding =
                new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.None)
                {
                    AllowCookies = false,
                    BypassProxyOnLocal = false,
                    CloseTimeout = new TimeSpan(0, 1, 0),
                    HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard,
                    MaxBufferPoolSize = 524288,
                    MaxBufferSize = 65536,
                    MaxReceivedMessageSize = 65536,
                    MessageEncoding = System.ServiceModel.WSMessageEncoding.Text,
                    Name = "AWSECommerceServiceBinding",
                    OpenTimeout = new TimeSpan(0, 1, 0),
                    ReceiveTimeout = new TimeSpan(0, 10, 0),
                    SendTimeout = new TimeSpan(0, 1, 0),
                    TextEncoding = Encoding.UTF8,
                    TransferMode = System.ServiceModel.TransferMode.Buffered,
                    UseDefaultWebProxy = true,
                    ReaderQuotas = InitReaderQuotas()
                };
            return binding;
        }

        /// <summary>
        /// Reader quotas for HttpBinding
        /// </summary>
        /// <returns>XmlDictionaryReaderQuotas for binding</returns>
        private static System.Xml.XmlDictionaryReaderQuotas InitReaderQuotas()
        {
            System.Xml.XmlDictionaryReaderQuotas quotas = new System.Xml.XmlDictionaryReaderQuotas
            {
                MaxArrayLength = 16384,
                MaxDepth = 32,
                MaxNameTableCharCount = 16384,
                MaxStringContentLength = 8192,
                MaxBytesPerRead = 4096
            };
            return quotas;
        }

        #endregion
    }
}
